package com.ruoyi.archive.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.archive.domain.SSoldierBaseInfo;
import com.ruoyi.archive.mapper.SSoldierBaseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.archive.mapper.STransferredCadresMapper;
import com.ruoyi.archive.domain.STransferredCadres;
import com.ruoyi.archive.service.ISTransferredCadresService;

/**
 * 军队转业干部信息Service业务层处理
 *
 * @author Allen
 * @date 2021-09-28
 */
@Service
public class STransferredCadresServiceImpl extends ServiceImpl<STransferredCadresMapper, STransferredCadres> implements ISTransferredCadresService {
    @Autowired
    private STransferredCadresMapper sTransferredCadresMapper;

    @Autowired
    private SSoldierBaseInfoMapper sSoldierBaseInfoMapper;

    /**
     * 查询军队转业干部信息
     *
     * @param id
     *         军队转业干部信息主键
     *
     * @return 军队转业干部信息
     */
    @Override
    public STransferredCadres selectSTransferredCadresById(Long id) {
        return sTransferredCadresMapper.selectSTransferredCadresById(id);
    }

    /**
     * 查询军队转业干部信息列表
     *
     * @param sTransferredCadres
     *         军队转业干部信息
     *
     * @return 军队转业干部信息
     */
    @Override
    public List<STransferredCadres> selectSTransferredCadresList(STransferredCadres sTransferredCadres) {
        List<STransferredCadres> transferredCadresList = sTransferredCadresMapper.selectSTransferredCadresList(sTransferredCadres);
        if (CollectionUtil.isNotEmpty(transferredCadresList)) {
            List<Long> soldierIdList = transferredCadresList.stream().map(STransferredCadres::getSoldierId).collect(Collectors.toList());
            Map<Long, SSoldierBaseInfo> baseInfoMap = getSoldierBaseInfoMap(soldierIdList);
            transferredCadresList.stream().forEach(x -> x.setSoldierName(baseInfoMap.get(x.getSoldierId()).getName()));
            transferredCadresList.stream().forEach(x -> x.setIdCard(baseInfoMap.get(x.getSoldierId()).getIdCard()));
        }
        return transferredCadresList;
    }

    @Override
    public Map<Long, SSoldierBaseInfo> getSoldierBaseInfoMap(List<Long> soldierIdList) {
        return sSoldierBaseInfoMapper.selectBatchIds(soldierIdList).stream().collect(Collectors.toMap(SSoldierBaseInfo::getId, y -> y));
    }

    /**
     * 新增军队转业干部信息
     *
     * @param sTransferredCadres
     *         军队转业干部信息
     *
     * @return 结果
     */
    @Override
    public int insertSTransferredCadres(STransferredCadres sTransferredCadres) {
        if (sTransferredCadres != null) {
            return sTransferredCadresMapper.insertSTransferredCadres(sTransferredCadres);
        }
        return 0;
    }

    /**
     * 修改军队转业干部信息
     *
     * @param sTransferredCadres
     *         军队转业干部信息
     *
     * @return 结果
     */
    @Override
    public int updateSTransferredCadres(STransferredCadres sTransferredCadres) {
        return sTransferredCadresMapper.updateSTransferredCadres(sTransferredCadres);
    }

    /**
     * 批量删除军队转业干部信息
     *
     * @param ids
     *         需要删除的军队转业干部信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSTransferredCadresByIds(Long[] ids) {
        return sTransferredCadresMapper.deleteSTransferredCadresByIds(ids);
    }

    /**
     * 删除军队转业干部信息信息
     *
     * @param id
     *         军队转业干部信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSTransferredCadresById(Long id) {
        return sTransferredCadresMapper.deleteSTransferredCadresById(id);
    }
}
